package com.yileiqian.alg;

public class FindLocateMin {

	public static int findLocateMin(int[] array) {
		if (array.length == 0) {
			throw new IllegalArgumentException();
		}
		if (array.length == 1) {
			return array[0];
		}
		return findLocateMin(array, 0, array.length - 1);
	}

	private static int findLocateMin(int[] array, int left, int right) {
		if (left == right) {
			return array[left];
		}
		int mid = left + (right - left) / 2;
		if (mid == 0) {
			return array[mid] < array[right] ? array[mid] : array[right];
		}
		if (array[mid] < array[mid - 1] && array[mid] < array[mid + 1]) {
			return array[mid];
		}
		if (array[mid] < array[mid - 1]) {
			return findLocateMin(array, mid + 1, right);
		}
		return findLocateMin(array, left, mid - 1);
	}

	public static void main(String args[]) {
		int array[] = {5,5,5,5,5 };
		System.out.println(findLocateMin(array));
	}
}
